//
//  RegisterViewController.m
//  QuickCure1
//
//  Created by maoge on 16/10/30.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import <SMS_SDK/SMSSDK.h>

#import "RegisterViewController.h"
#import "LoginViewController.h"

#import "InputUserDetailViewController.h"
#import "Users.h"

#import "SSKeychain.h"

#import <SMS_SDK/SMSSDK.h>

@interface RegisterViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textFieldTelNum;
@property (weak, nonatomic) IBOutlet UITextField *textFieldVerificationCode;
@property (weak, nonatomic) IBOutlet UITextField *textFieldPassword;
@property (weak, nonatomic) IBOutlet UIButton *buttonNextStep;
@property (weak, nonatomic) IBOutlet UIButton *buttonUserConfirmAgreement;
@property (weak, nonatomic) IBOutlet UIButton *buttonGetVerification;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    [self setupUI];
    
}

- (void)setupUI{

    self.navigationItem.title = @"用户注册";
    
    _buttonNextStep.enabled =NO;
    
    self.textFieldTelNum.leftView = [[UIView alloc] initWithFrame:CGRectMake(2, 2, 26, 26)];
    self.textFieldTelNum.leftViewMode = UITextFieldViewModeAlways;
    self.textFieldTelNum.delegate = self;
    
    
    self.textFieldVerificationCode.leftView =[[UIView alloc] initWithFrame:CGRectMake(2, 2, 26, 26)];
    self.textFieldVerificationCode.leftViewMode = UITextFieldViewModeAlways;
    self.textFieldVerificationCode.delegate = self;
    
    self.textFieldPassword.leftView =[[UIView alloc] initWithFrame:CGRectMake(2, 2, 26, 26)];
    self.textFieldPassword.leftViewMode = UITextFieldViewModeAlways;
    self.textFieldPassword.delegate = self;


}


- (IBAction)getVerificationCode:(UIButton *)sender {
    
  
    
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:_textFieldTelNum.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
    
            [self presentAlertWithError:error];
        
            [self buttonVerificationCountDownWithButton:sender];
   
    }];
    
    
    
}

- (void)presentAlertWithError:(NSError *)error{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle: error ? @"失败" : @"成功" message: error ? @"请重新获取" : @"请查收短息" preferredStyle:UIAlertControllerStyleAlert];
    
    [self presentViewController:alert animated:YES completion:^{
        
        [UIView animateWithDuration:1 animations:^{
            
            alert.view.alpha = 0;
            
        } completion:^(BOOL finished) {
            
            [alert dismissViewControllerAnimated:YES completion:nil];
        }];

    }];

}



- (void)buttonVerificationCountDownWithButton:(UIButton *)button{
    
    button.enabled = NO;
    
    button.backgroundColor = [UIColor lightGrayColor];
    
   __block NSInteger i = 15;
    
    NSTimer *timer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
       
        [button setTitle:[NSString stringWithFormat:@"重新获取(%zd)",i] forState:UIControlStateNormal];
        
        if (i == 0) {
            
             timer.fireDate = [NSDate distantFuture];
            
            
            button.enabled = YES;
             [button setTitle:[NSString stringWithFormat:@"重新获取",i] forState:UIControlStateNormal];
            
            button.backgroundColor = kThemeColor;
            
        }  else {
        
            i--;
        }
        
       
    }];
    
    
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
    
}





- (IBAction)nextStep:(UIButton *)sender {
    [SMSSDK commitVerificationCode:self.textFieldVerificationCode.text phoneNumber:self.textFieldTelNum.text zone:@"86" result:^(SMSSDKUserInfo *userInfo, NSError *error) {
        if (!error) {
            NSLog(@"验证成功");
            [self pushUserDetailVC];
        }
        else
        {
//            NSLog(@"验证失败");
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"验证码错误" message:@"请重新输入" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            [alert addAction:sure];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
    
    
    
}

- (IBAction)clickToRegisterController:(UIButton *)sender {
    
    [self pushRegisterVC];
    
}

- (IBAction)userAgreement:(UIButton *)sender {
    
}



- (IBAction)clickToConfirmUserAgreement:(UIButton *)sender {
    
    sender.selected = !sender.isSelected;
    
    [self enableNextStepButton];
    
}



-(void)pushUserDetailVC{
    
    InputUserDetailViewController *userDetailVC = [[UIStoryboard storyboardWithName:@"InputUserDetailViewController" bundle:nil] instantiateInitialViewController];;
    
    Users *model = [[Users alloc] init];
    
    model.mobile_number = @([_textFieldTelNum.text integerValue]);
    
    userDetailVC.model = model;
    
    [self.navigationController pushViewController:userDetailVC animated:YES];
}


-(void)pushRegisterVC{
    
    LoginViewController *loginVC = [[UIStoryboard storyboardWithName:@"LoginViewController" bundle:nil] instantiateInitialViewController];;
    
    [self.navigationController pushViewController:loginVC animated:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    
    [self enableNextStepButton];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    return [textField resignFirstResponder];
    
}



- (void)enableNextStepButton{
    
    
    if ([_textFieldTelNum.text isEqualToString:@""] || [_textFieldVerificationCode.text isEqualToString:@""] || [_textFieldPassword.text isEqualToString:@""]) {
        
        _buttonNextStep.enabled = NO;
        _buttonNextStep.backgroundColor = [UIColor lightGrayColor];
        
    } else if (_buttonUserConfirmAgreement.isSelected){
        
        _buttonNextStep.enabled = YES;
        
        _buttonNextStep.backgroundColor = [UIColor colorWithRed:24/255.0 green:188/255.0 blue:187/255.0 alpha:1];
    }
    
}

@end
